package com.example.smartwaterapi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.smartwaterapi.common.api.RestApiResponse;
import com.example.smartwaterapi.entity.UserWatchface;
import com.example.smartwaterapi.entity.Watchface;
import com.example.smartwaterapi.service.WatchfaceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表盘控制器
 */
@Tag(name = "表盘管理API", description = "智能手表表盘商店、下载、收藏等相关接口")
@RestController
@RequestMapping("/api/watchface")
public class WatchfaceController {

    @Autowired
    private WatchfaceService watchfaceService;

    /**
     * 分页获取表盘列表
     */
    @Operation(summary = "获取表盘商店列表", description = "分页获取表盘商店列表，支持分类、类型、价格类型等多种过滤条件")
    @GetMapping("/list")
    public RestApiResponse<Page<Watchface>> getWatchfaceList(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "表盘分类", example = "运动") @RequestParam(required = false) String category,
            @Parameter(description = "表盘类型：1-官方表盘，2-DIY表盘") @RequestParam(required = false) Integer type,
            @Parameter(description = "价格类型：0-免费，1-付费，2-会员专享，3-积分兑换") @RequestParam(required = false) Integer priceType,
            @Parameter(description = "搜索关键字") @RequestParam(required = false) String keyword,
            @Parameter(description = "排序字段：createTime-创建时间，downloadCount-下载量，ratingAvg-评分") @RequestParam(required = false) String sortField,
            @Parameter(description = "排序方式：asc-升序，desc-降序") @RequestParam(required = false) String sortOrder) {

        Map<String, Object> params = new HashMap<>();
        if (category != null) params.put("category", category);
        if (type != null) params.put("type", type);
        if (priceType != null) params.put("priceType", priceType);
        if (keyword != null) params.put("keyword", keyword);
        if (sortField != null) params.put("sortField", sortField);
        if (sortOrder != null) params.put("sortOrder", sortOrder);

        Page<Watchface> watchfacePage = watchfaceService.getWatchfaceList(page, size, params);
        return RestApiResponse.ok(watchfacePage);
    }

    /**
     * 获取表盘详情
     */
    @Operation(summary = "获取表盘详情", description = "根据表盘ID获取表盘的详细信息，包括预览图、文件等")
    @GetMapping("/{id}")
    public RestApiResponse<Watchface> getWatchfaceDetail(
            @Parameter(description = "表盘ID") @PathVariable Long id) {
        Watchface watchface = watchfaceService.getWatchfaceDetail(id);
        return RestApiResponse.ok(watchface);
    }

    /**
     * 获取推荐表盘
     */
    @Operation(summary = "获取推荐表盘", description = "获取平台推荐的精品表盘列表")
    @GetMapping("/featured")
    public RestApiResponse<List<Watchface>> getFeaturedWatchfaces(
            @Parameter(description = "限制数量", example = "6") @RequestParam(defaultValue = "6") Integer limit) {
        List<Watchface> watchfaces = watchfaceService.getFeaturedWatchfaces(limit);
        return RestApiResponse.ok(watchfaces);
    }

    /**
     * 获取热门表盘
     */
    @Operation(summary = "获取热门表盘", description = "获取下载量最高的热门表盘列表")
    @GetMapping("/popular")
    public RestApiResponse<List<Watchface>> getPopularWatchfaces(
            @Parameter(description = "限制数量", example = "6") @RequestParam(defaultValue = "6") Integer limit) {
        List<Watchface> watchfaces = watchfaceService.getPopularWatchfaces(limit);
        return RestApiResponse.ok(watchfaces);
    }

    /**
     * 用户下载表盘
     */
    @Operation(summary = "下载表盘", description = "用户下载表盘到个人库，支持免费、付费、会员专享、积分兑换等多种获取方式")
    @PostMapping("/download")
    public RestApiResponse<UserWatchface> downloadWatchface(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "表盘ID") @RequestParam Long watchfaceId) {
        UserWatchface userWatchface = watchfaceService.downloadWatchface(userId, watchfaceId);
        return RestApiResponse.ok(userWatchface);
    }

    /**
     * 用户收藏/取消收藏表盘
     */
    @Operation(summary = "收藏或取消收藏表盘", description = "用户对已下载的表盘进行收藏或取消收藏操作")
    @PostMapping("/favorite")
    public RestApiResponse<Boolean> toggleFavorite(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "表盘ID") @RequestParam Long watchfaceId,
            @Parameter(description = "是否收藏：true-收藏，false-取消收藏") @RequestParam Boolean favorite) {
        boolean result = watchfaceService.toggleFavorite(userId, watchfaceId, favorite);
        return RestApiResponse.ok(result);
    }

    /**
     * 获取用户表盘库
     */
    @Operation(summary = "获取用户表盘库", description = "分页获取用户已下载的所有表盘")
    @GetMapping("/user/{userId}")
    public RestApiResponse<Page<UserWatchface>> getUserWatchfaces(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        Page<UserWatchface> userWatchfaces = watchfaceService.getUserWatchfaces(userId, page, size);
        return RestApiResponse.ok(userWatchfaces);
    }

    /**
     * 获取用户收藏的表盘
     */
    @Operation(summary = "获取用户收藏表盘", description = "分页获取用户收藏的表盘列表")
    @GetMapping("/user/{userId}/favorite")
    public RestApiResponse<Page<UserWatchface>> getUserFavoriteWatchfaces(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        Page<UserWatchface> userWatchfaces = watchfaceService.getUserFavoriteWatchfaces(userId, page, size);
        return RestApiResponse.ok(userWatchfaces);
    }

    /**
     * 获取表盘分类列表
     */
    @Operation(summary = "获取表盘分类", description = "获取所有可用的表盘分类列表")
    @GetMapping("/categories")
    public RestApiResponse<List<String>> getWatchfaceCategories() {
        // 这里可以从数据库或配置中获取分类列表
        List<String> categories = List.of("运动", "商务", "时尚", "卡通", "简约", "个性");
        return RestApiResponse.ok(categories);
    }

    /**
     * 表盘评分
     */
    @Operation(summary = "表盘评分", description = "用户对表盘进行评分（1-5星）")
    @PostMapping("/{watchfaceId}/rate")
    public RestApiResponse<Boolean> rateWatchface(
            @Parameter(description = "表盘ID") @PathVariable Long watchfaceId,
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "评分(1-5)", example = "5") @RequestParam Integer rating) {
        if (rating < 1 || rating > 5) {
            return RestApiResponse.badRequest("评分必须在1-5之间");
        }
        // 这里需要实现评分功能，暂时返回成功
        return RestApiResponse.ok(true);
    }

    /**
     * 设置当前使用的表盘
     */
    @Operation(summary = "设置当前表盘", description = "用户设置当前正在使用的表盘")
    @PostMapping("/user/{userId}/current")
    public RestApiResponse<Boolean> setCurrentWatchface(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "表盘ID") @RequestParam Long watchfaceId) {
        // 这里需要实现设置当前表盘的功能，暂时返回成功
        return RestApiResponse.ok(true);
    }
}